package com.itheima.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.dto.ProductDto;
import com.itheima.excption.MyException;
import com.itheima.pojo.*;
import com.itheima.vo.PageResultVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class PmsProductService {

    @DubboReference
    private PmsProductApi pmsProductApi;

    @DubboReference
    private PmsMemberPriceApi pmsMemberPriceApi;

    @DubboReference
    private CmsPrefrenceAreaProductRelationApi cmsPrefrenceAreaProductRelationApi;

    @DubboReference
    private PmsProductAttributeValueApi pmsProductAttributeValueApi;

    @DubboReference
    private PmsProductFullReductionApi pmsProductFullReductionApi;

    @DubboReference
    private PmsProductLadderApi pmsProductLadderApi;

    @DubboReference
    private PmsSkuStockApi pmsSkuStockApi;

    @DubboReference
    private CmsSubjectProductRelationApi cmsSubjectProductRelationApi;

    /**
     * 查询商品
     */
    public PageResultVo<PmsProduct> list(Integer pageNum, Integer pageSize, Integer brandId, String keyword, Integer productCategoryId, String productSn, Integer publishStatus, Integer verifyStatus) {

        IPage<PmsProduct> page = pmsProductApi.list(pageNum, pageSize, brandId,
                keyword, productCategoryId, productSn,
                publishStatus, verifyStatus);

        PageResultVo<PmsProduct> pageResultVo = new PageResultVo<>();
        pageResultVo.setList(page.getRecords());
        pageResultVo.setPageNum((int) page.getCurrent());
        pageResultVo.setPageSize((int) page.getSize());
        pageResultVo.setTotal((int) page.getTotal());
        pageResultVo.setTotalPages((int) page.getPages());

        return pageResultVo;
    }

    /**
     * 根据商品名称或货号模糊查询
     */
    public List<PmsProduct> simpleList(String keyword) {
        List<PmsProduct> list = pmsProductApi.simpleList(keyword);
        return list;
    }

    /**
     * 批量修改删除状态
     */
    public void deleteStatus(Integer deleteStatus, List<Integer> ids) {
        pmsProductApi.deleteStatus(deleteStatus, ids);
    }

    /**
     * 批量设为新品
     */
    public void newStatus(Integer newStatus, List<Integer> ids) {
        Integer flag = pmsProductApi.newStatus(newStatus, ids);

        if (flag == 1) {
            throw new MyException("部分商品审核尚未通过!");
        }

        if (flag == 2) {
            throw new MyException("部分商品未上架!");
        }
    }

    /**
     * 批量上下架商品
     */
    public void publishStatus(Integer publishStatus, List<Integer> ids) {
        Integer flag = pmsProductApi.publishStatus(publishStatus, ids);
        if (flag == 1) {
            throw new MyException("部分商品审核尚未通过!");
        }
    }

    /**
     * 批量推荐商品
     */
    public void recommendStatus(Integer recommendStatus, List<Integer> ids) {
        Integer flag = pmsProductApi.recommendStatus(recommendStatus, ids);

        if (flag == 1) {
            throw new MyException("部分商品审核尚未通过!");
        }

        if (flag == 2) {
            throw new MyException("部分商品未上架!");
        }
    }

    /**
     * 批量修改审核状态
     */
    public void verifyStatus(Integer verifyStatus, List<Integer> ids) {
        pmsProductApi.verifyStatus(verifyStatus, ids);
    }


    /**
     * 创建商品
     */
    public void create(ProductDto productParam) {

        PmsProduct pmsProduct = new PmsProduct();
        BeanUtils.copyProperties(productParam, pmsProduct);
        PmsProduct product = pmsProductApi.save(pmsProduct);

        if (ObjectUtils.isEmpty(product)) {
            throw new MyException("该商品已存在,不可重复添加!");
        }

        Long productId = product.getId();

        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = productParam.getPrefrenceAreaProductRelationList();
        cmsPrefrenceAreaProductRelationApi.save(prefrenceAreaProductRelationList, productId);

        List<CmsSubjectProductRelation> subjectProductRelationList = productParam.getSubjectProductRelationList();
        cmsSubjectProductRelationApi.save(subjectProductRelationList, productId);

        List<PmsProductLadder> productLadderList = productParam.getProductLadderList();
        pmsProductLadderApi.save(productLadderList, productId);

        List<PmsProductAttributeValue> productAttributeValueList = productParam.getProductAttributeValueList();
        pmsProductAttributeValueApi.save(productAttributeValueList, productId);

        List<PmsProductFullReduction> productFullReductionList = productParam.getProductFullReductionList();
        pmsProductFullReductionApi.save(productFullReductionList, productId);

        List<PmsMemberPrice> memberPriceList = productParam.getMemberPriceList();
        pmsMemberPriceApi.save(memberPriceList, productId);

        List<PmsSkuStock> skuStockList = productParam.getSkuStockList();
        pmsSkuStockApi.save(skuStockList, productId);

    }

    /**
     * 根据商品id获取商品编辑信息
     */
    public ProductDto updateInfo(Integer productId) {

        ProductDto productDto = new ProductDto();

        PmsProduct pmsProduct = pmsProductApi.updateInfo(productId);

        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = cmsPrefrenceAreaProductRelationApi.updateInfo(productId);

        List<CmsSubjectProductRelation> subjectProductRelationList = cmsSubjectProductRelationApi.updateInfo(productId);

        List<PmsProductLadder> productLadderList = pmsProductLadderApi.updateInfo(productId);

        List<PmsProductAttributeValue> productAttributeValueList = pmsProductAttributeValueApi.updateInfo(productId);

        List<PmsProductFullReduction> productFullReductionList = pmsProductFullReductionApi.updateInfo(productId);

        List<PmsMemberPrice> memberPriceList = pmsMemberPriceApi.updateInfo(productId);

        List<PmsSkuStock> skuStockList = pmsSkuStockApi.updateInfo(productId);

        BeanUtils.copyProperties(pmsProduct, productDto);
        productDto.setPrefrenceAreaProductRelationList(prefrenceAreaProductRelationList);
        productDto.setSubjectProductRelationList(subjectProductRelationList);
        productDto.setProductLadderList(productLadderList);
        productDto.setProductAttributeValueList(productAttributeValueList);
        productDto.setProductFullReductionList(productFullReductionList);
        productDto.setMemberPriceList(memberPriceList);
        productDto.setSkuStockList(skuStockList);

        return productDto;
    }

    /**
     * 更新商品
     */
    public void updateProduct(Integer id, ProductDto productParam) {
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtils.copyProperties(productParam, pmsProduct);
        Long productId = Long.valueOf(id);
        pmsProduct.setId(Long.valueOf(productId));

        boolean flag = pmsProductApi.updateProduct(pmsProduct);

        if (flag) {
            throw new MyException("商品名称重复,请重试!");
        }

        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = productParam.getPrefrenceAreaProductRelationList();
        cmsPrefrenceAreaProductRelationApi.updateProduct(prefrenceAreaProductRelationList, productId);

        List<CmsSubjectProductRelation> subjectProductRelationList = productParam.getSubjectProductRelationList();
        cmsSubjectProductRelationApi.updateProduct(subjectProductRelationList, productId);

        List<PmsProductLadder> productLadderList = productParam.getProductLadderList();
        pmsProductLadderApi.updateProduct(productLadderList, productId);

        List<PmsProductAttributeValue> productAttributeValueList = productParam.getProductAttributeValueList();
        pmsProductAttributeValueApi.updateProduct(productAttributeValueList, productId);

        List<PmsProductFullReduction> productFullReductionList = productParam.getProductFullReductionList();
        pmsProductFullReductionApi.updateProduct(productFullReductionList, productId);

        List<PmsMemberPrice> memberPriceList = productParam.getMemberPriceList();
        pmsMemberPriceApi.updateProduct(memberPriceList, productId);

        List<PmsSkuStock> skuStockList = productParam.getSkuStockList();
        pmsSkuStockApi.updateProduct(skuStockList, productId);

    }
}
